﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TextureUtil {

    public static Dictionary<int, Sprite> CreateSpitesByTexture(Texture2D srcT2d,int row,int col)
    {
        //row 行 cow列
        Dictionary<int, Sprite> sprites = new Dictionary<int, Sprite> ();
        int w = srcT2d.width / col;
        int h = srcT2d.height / row;
        int index = 1;
        for (int ir = col-1; ir >=0 ; ir--)
        {
            for (int ic = 0; ic < col; ic++)
            {
                Sprite sp = Sprite.Create(srcT2d, new Rect(w* ic,h* ir, w, h),new Vector2(0.5f,0.5f));
                sprites[index] = sp;
                index++;
            }
        }
        return sprites;
    }

    internal static Texture2D GetTextureByName(string name)
    {
        Texture2D t2d = new Texture2D(1, 1);
        byte[] bs = FileUtil.ReadBytes(name);
        if (bs != null)
        {
            t2d.LoadImage(bs);
            return t2d;
        }
        return null;
    }

    internal static Texture2D GetTextureByBytes(byte[] bs)
    {
        Texture2D t2d = new Texture2D(1, 1);
        if (bs != null)
        {
            t2d.LoadImage(bs);
            return t2d;
        }
        return null;
    }

}
